﻿Ext.namespace('ui');

function showMainPage() {
	if (!sys.dataStruct) {
		ui.problemList = getProblemListPanel();
		ui.noticeList = getNoticeListPanel();
		ui.ctrlBox = getCtrlBoxPanel();
		ui.statusBar = getStatusBar();
		ui.tabPages = new Ext.TabPanel({
					region : 'center',
					bodyBorder : false,
					layoutOnTabChange : true,
					activeItem : 0,
					items : ui.problemList,

					initEvents : function() {
						Ext.TabPanel.superclass.initEvents.call(this);
						this.mon(this.strip, {
									scope : this,
									mousedown : this.onStripMouseDown,
									contextmenu : this.onStripContextMenu
								});
						if (this.enableTabScroll) {
							this.mon(this.strip, 'mousewheel', this.onWheel, this);
						}
						// ADD:monitor title dbclick
						this.mon(this.strip, 'dblclick', this.onTitleDbClick, this);
					},
					// ADD: handler
					onTitleDbClick : function(e, target, o) {
						var t = this.findTargets(e);
						if (t.item.fireEvent('beforeclose', t.item) !== false) {
							t.item.fireEvent('close', t.item);
							this.remove(t.item);
						}
					}
				});

		var viewPort = new Ext.Viewport({
					layout : 'border',
					items : [new Ext.BoxComponent({
										region : 'north',
										el : 'header',
										height : 32
									}), ui.tabPages, new Ext.Panel({
										title : '<center>通知公告</center>',
										region : 'west',
										width : 250,
										minWidth : 225,
										layout : 'border',
										split : true,
										collapsible : true,
										collapseMode : 'mini',
										items : [ui.noticeList, ui.ctrlBox]
									}), ui.statusBar]
				});
	} else {
		ui.showExperimentList = showExperimentList();
		ui.noticeList = getDSNoticeListPanel();
		ui.ctrlBox = getCtrlBoxPanel();
		ui.statusBar = getStatusBar();
		ui.windows = new Ext.WindowGroup();
		ui.tabPages = new Ext.TabPanel({
					region : 'center',
					bodyBorder : false,
					layoutOnTabChange : true,
					activeItem : 0,
					items : ui.showExperimentList,
					initEvents : function() {
						Ext.TabPanel.superclass.initEvents.call(this);
						this.mon(this.strip, {
									scope : this,
									mousedown : this.onStripMouseDown,
									contextmenu : this.onStripContextMenu
								});
						if (this.enableTabScroll) {
							this.mon(this.strip, 'mousewheel', this.onWheel, this);
						}
						// ADD:monitor title dbclick
						this.mon(this.strip, 'dblclick', this.onTitleDbClick, this);
					},
					// ADD: handler
					onTitleDbClick : function(e, target, o) {
						var t = this.findTargets(e);
						if (t.item.fireEvent('beforeclose', t.item) !== false) {
							t.item.fireEvent('close', t.item);
							this.remove(t.item);
						}
					}
				});

		var viewPort = new Ext.Viewport({
					layout : 'border',
					items : [new Ext.BoxComponent({
										region : 'north',
										el : 'header',
										height : 32
									}), ui.tabPages, new Ext.Panel({
										title : '<center>通知公告</center>',
										region : 'west',
										width : 250,
										minWidth : 225,
										layout : 'border',
										split : true,
										collapsible : true,
										collapseMode : 'mini',
										items : [ui.noticeList, ui.ctrlBox]
									}), ui.statusBar]
				});
	}
}

function refreshMainPage(handler) {
	getSystemSettings(function() {
				getCurrentUserInfo(function() {
							refreshCtrlBox();
							refreshStatusBar();
							if (sys.dataStruct == false) {
								refreshNoticeList();
								refreshProblemList();
								refreshTab('showStatusList');
								refreshTab('showRanklist');
							} else {
								refreshDSNoticeList();
							}
							if (handler) handler();
						})
			})
}

function addTab(obj) {
	var tab = ui.tabPages.getItem(obj.id);
	if (!tab) tab = ui.tabPages.add(obj);
	ui.tabPages.setActiveTab(tab);
	return tab;
}
function closeTab(id) {
	var tab = ui.tabPages.getItem(id);
	if (tab) return ui.tabPages.remove(tab);
	return false;
}

function refreshTab(id, obj, noactive) {
	var tab = ui.tabPages.getItem(id);
	if (tab) {
		if (!noactive) ui.tabPages.setActiveTab(tab);
		if (tab.onRefresh) tab.onRefresh(obj);
	}
	return tab;
}

function refreshWin(id, obj, noactive) {
	var win = Ext.WindowMgr.get(id);
	if (win) {
		if (!noactive) win.show();
		if (win.onRefresh) win.onRefresh(obj);
	}
	return win;
}
